import java.util.*;

/**
 * @ClassName Offer32
 * @Description 剑指 Offer 32 - I. 从上到下打印二叉树
 * @Author clockTown
 * @Date 2021/8/17 9:11
 * @Version 1.0
 */
class Offer32I {
}

/**
 * BFS
 */
class Offer32IV0 {
    public int[] levelOrder(TreeNode root) {
        if (root == null){
            return new int[0];
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        List<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()){
            TreeNode poll = queue.poll();
            list.add(poll.val);
            if (poll.left != null){
                queue.offer(poll.left);
            }
            if (poll.right != null){
                queue.offer(poll.right);
            }
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}